package com.zhou.JavaAndroidFirm.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.base.BaseActivity;
import com.zhou.JavaAndroidFirm.utils.SharedPreferenceUtils;

public class SplashActivity extends BaseActivity {
    Handler handler;
    Intent intent;
    private boolean isFirst;
    private RxPermissions rxPermissions;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void initView(Bundle bundle) {
        //这步取值操作不能放在run中，否则会执行两次导致页面跳转出错
        isFirst = SharedPreferenceUtils.getBooleanData("isFirst", true);
        handler = new Handler();
        rxPermissions = new RxPermissions(SplashActivity.this); // where this is an Activity or Fragment instance
        //检查动态权限
        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE
                , Manifest.permission.WRITE_EXTERNAL_STORAGE
        ).subscribe();
    }

    @Override
    protected void initEvent() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isFinishing()) {
                    return;
                }
                if (isFirst) {
                    intent = new Intent(SplashActivity.this, GuideActivity.class);
                    SharedPreferenceUtils.setBooleanData("isFirst", false);

                } else {
                    intent = new Intent(SplashActivity.this, MainActivity.class);
                }
                startActivity(intent);
                finish();
            }
        }, 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }
}
